package com.dwxt.race.service;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.dwxt.common.zfbpay.ZFBConfig;
import com.dwxt.race.dao.RaceMapper;
import com.dwxt.race.dao.RaceOrderMapper;
import com.dwxt.race.dao.UserRaceMapper;
import com.dwxt.race.entity.RaceOrder;
import com.dwxt.race.entity.UserRace;
import com.dwxt.race.entity.UserRaceExample;
import com.github.pagehelper.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ZfbPayService {

    @Autowired
    private RaceOrderMapper raceOrderMapper;
    @Autowired
    private RaceMapper raceMapper;
    @Autowired
    private UserRaceService userRaceService;
    @Autowired
    private UserRaceMapper userRaceMapper;

    public String checkParamsZFB(Map<String, String> params) throws AlipayApiException {
        //PayAccount account = payAccountService.selectEffectivePayAccount(PurchaseMethod.ZFB);
        boolean signVerified = AlipaySignature.rsaCheckV1(params, ZFBConfig.publicKey, ZFBConfig.charset, ZFBConfig.signType);
        //对验签进行处理
        log.info("result:1");
        if (signVerified) {  //验签通过
            String out_trade_no = params.get("out_trade_no");// 商户订单号
            if (StringUtil.isNotEmpty(out_trade_no)){
                UserRaceExample userRaceExample = new UserRaceExample();
                UserRaceExample.Criteria criteria = userRaceExample.createCriteria();
                criteria.andOrderIdEqualTo(out_trade_no);
                List<UserRace> userRaces = userRaceMapper.selectByExample(userRaceExample);
                if (userRaces.size()>0){
                    UserRace userRace = userRaces.get(0);
                    Integer raceId = userRace.getRaceId();
                    String userId = userRace.getUserId();
                    if ("TRADE_SUCCESS".equals(params.get("trade_status"))) {//只处理支付成功的订单: 修改交易表状态,支付成功
                        RaceOrder raceOrder = new RaceOrder();
                        raceOrder.setUserid(userId);
                        raceOrder.setRealprice(raceMapper.selectByPrimaryKey(raceId).getCost());
                        raceOrder.setPaytype(1);
                        raceOrder.setStatus(1);
                        raceOrder.setRaceid(raceId);
                        raceOrder.setId(out_trade_no);
                        //支付成功后生成订单
                        raceOrderMapper.insertSelective(raceOrder);
                        userRace.setStatus(1);
                        userRaceMapper.updateByPrimaryKeySelective(userRace);
                        return "success";
                    } else {
                        //库存增加,订单失败
                        raceMapper.updateByPayBack(raceId,1);
                        //支付失败
                        userRace.setStatus(5);
                        userRaceMapper.updateByPrimaryKeySelective(userRace);
                        return "success";
                    }
                }
            }


        }
        log.info("result:5");
        return "fail";
    }

}
